define(["java/like/jquery"],function($){
	var Document = Java.type("org.bson.Document");
	var Client = Java.type("com.mongodb.MongoClient");
	var ObjectId = Java.type("org.bson.types.ObjectId");
	var BasicDBObject = Java.type("com.mongodb.BasicDBObject");
	var that = this;
	function toDoc(obj){
		var doc;
		if(obj.id){
			var oid = new ObjectId(obj.id);
			doc = new BasicDBObject("_id",oid);
		}else{
			doc = new Document();
		}
		return $.extend(doc,obj);
	}
	function formatObj(doc){
		var oid = doc.getObjectId("_id");
		var id = oid.toHexString();
		doc.id = id;
		return doc;
	}
	function Collection(host,dbName,collName){
		
		this.client = new Client(host);
		this.db = this.client.getDatabase(dbName);
		this.coll = this.db.getCollection(collName);
		this.close = function(){
			this.client.close();
		}

	}
	Collection.prototype.save = function(param){
		if(param){
			var doc ;
			if($.isArray(param)){
				for (var i = 0; i < param.length; i++) {
					if(param[i].id == undefined){
						doc = toDoc(param[i]);
						this.coll.insertOne(doc);
					} else {
						// this.coll.updateOne(toDoc({_id:doc._id}),doc);
						this.updateById(doc.id,[i]);
					}
				}
			} else {
				if(param.id == undefined){
					doc = toDoc(param);
					this.coll.insertOne(doc);
				} else {
					// this.coll.updateOne(toDoc({_id:doc._id}),doc);
					this.updateById(param.id,param);
				}
				
			}
			return "success";
		}
	};
	Collection.prototype.update = function(query,data){
		if(query && data){
			this.coll.updateMany(toDoc(query),toDoc(data));
			return "success";
		} else {
			return ;
		}
	}
	Collection.prototype.updateById = function(id,data){
		if(id && data){
			var source = this.findById(id);
			var target = $.extend(source,data);
			var oid = new ObjectId(id);
			var obj = new BasicDBObject("_id",oid);
			target.updateTime = new Date();
			INFO("TARGET　json :"+target.toString());
			return this.coll.findOneAndReplace(obj,target);
		} else {
			return ;
		}
	}
	Collection.prototype.find = function(query,page,sort){
		var list = new Array();
		var documents;
		if(query){
			var doc = toDoc(query);
			documents =this.coll.find(doc);
		} else {
			documents =this.coll.find();
		}
		if(page){
			documents =documents.skip(page.limit*(page.page-1)).limit(page.limit);
		}
		if(sort){
			documents = documents.sort(toDoc(sort));
		}

		if(documents){
			var it = documents.iterator();
			while(it.hasNext()){
				var doc = formatObj(it.next());
				list.push(doc);
			}
		}
		return list;
	};
	Collection.prototype.findById = function(id){
		var oid = new ObjectId(id);
		var obj = new BasicDBObject("_id",oid);
		var res = this.coll.find(obj);
		
		var it = res.iterator();
		if(it.hasNext()){
			return formatObj(it.next());
		}
		return undefined;
	}
	Collection.prototype.remove = function(param){
		this.coll.deleteMany(toDoc(param));
		return "success";
	};
	Collection.prototype.removeById = function(param){
		this.coll.deleteMany(toDoc(param));
		return "success";
	};
	return Collection;
});
